﻿using System;
using FSky;
using static System.Net.Mime.MediaTypeNames;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;//提供画gdi+图形的基本功能 
using System.Drawing.Text;//提供画gdi+图形的高级功能 
using System.Drawing.Drawing2D;//提供画高级二维，矢量图形功能 
using System.Drawing.Imaging;//提供画gdi+图形的高级功能  
using System.Media;
namespace idea0
{
    public class World
    {
        public Class1 BlackWalls { get; set; }
        public Class1_child WhitePieces { get; set; }
        public Class2? Player { get; set; }
        public enum GameResult//表示游戏的输赢结果
        {
            PlayerWin,
            PlayerLose
        }
        public event Action<GameResult> OnGameEnded;//游戏结束时触发
        public List<Bomb> Bombs = new List<Bomb>();
        public DateTime lastBombTime = DateTime.MinValue;// 记录上一次释放炸弹的时间
        public static readonly int BOMB_COOLDOWN = 2500;
        public PictureBox pictureBox1;
        public List<Class2_AI> AIs = new List<Class2_AI>(); // AI列表
        public List<ExplosionEffect> ExplosionEffects = new List<ExplosionEffect>();
        public World(PictureBox pictureBox)
        {
            BlackWalls = new Class1();
            WhitePieces = new Class1_child(BlackWalls);
            Player = new Class2(BlackWalls, this);
            pictureBox1 = pictureBox;
            InitializeAIs();
        }
        private void InitializeAIs()
        {
            // 创建AI实例并添加到列表中
            for (int i = 0; i < 3; i++) // 假设创建3个AI
            {
                Class2_AI ai = new Class2_AI(BlackWalls, this);
                AIs.Add(ai);
            }
        }
        //该方法检查指定格子是否有未爆炸的炸弹
        public bool IsBombPresent(int x, int y)
        {
            return Bombs.Any(b => b.X == x && b.Y == y && !b.Exploded);
        }

        public void Draw()
        {
            Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            Graphics g = Graphics.FromImage(bmp);

            // 绘制黑墙
            BlackWalls.Draw(g);

            // 绘制白块
            WhitePieces.Draw(g, Class2.cellSize);

            // 绘制炸弹
            foreach (Bomb bomb in Bombs)
            {
                bomb.Draw(g);
            }

            // 绘制爆炸效果
            foreach (ExplosionEffect effect in ExplosionEffects)
            {
                int x = effect.X * Class2.cellSize;
                int y = effect.Y * Class2.cellSize;
                g.DrawImage(effect.Image, x, y, Class2.cellSize, Class2.cellSize);
            }

            // 绘制AI
            foreach (Class2_AI ai in AIs)
            {
                ai.Draw(g);
                int x = ai.X - ai.W / 2;
                int y = ai.Y - ai.H / 2 - 5;
                int bloodWidth = (int)((ai.Life / (float)ai.MaxLife) * ai.W);
                g.FillRectangle(Brushes.Red, x, y, bloodWidth, 5);
            }

            // 绘制玩家
            if (Player != null)
            {
                Player.Draw(g);
            }

            pictureBox1.Image = bmp;
        }
        public bool IsGameRunning { get; set; } = true;
        public void Update()
        {
            if (Player != null)//检测游戏结束的逻辑
            {
                if (Player.Life <= 0 && AIs.Count > 0)
                {
                    IsGameRunning = false;
                    OnGameEnded?.Invoke(GameResult.PlayerLose);
                }
                else if (Player.Life >= 1 && AIs.Count == 0)
                {
                    IsGameRunning = false;
                    OnGameEnded?.Invoke(GameResult.PlayerWin);
                }
            }
            // Update AIs
            foreach (Class2_AI ai in AIs)
            {
                ai.Update(this);
            }

            // Update player
            if (Player != null)
            {
                Player.Update(this);
            }

            // Check player death
            if (Player != null && Player.Life <= 0)
            {
                Player = null;
            }

            // Check player picking up power-ups
            if (Player != null)
            {
                int playerGridX = Player.GridPosition.Item1;
                int playerGridY = Player.GridPosition.Item2;
                if (WhitePieces.IsWhiteCell(playerGridX, playerGridY))
                {
                    Player.PickUpPowerUp(playerGridX, playerGridY);
                }
            }

            // Update bombs
            UpdateBombs();

            // Update explosion effects
            List<ExplosionEffect> effectsToRemove = new List<ExplosionEffect>();
            foreach (ExplosionEffect effect in ExplosionEffects)
            {
                if (effect.IsExpired())
                {
                    effectsToRemove.Add(effect);
                }
            }
            foreach (var effect in effectsToRemove)
            {
                ExplosionEffects.Remove(effect);
            }
            // 检查AI死亡
            List<Class2_AI> aIsToRemove = new List<Class2_AI>();
            foreach (Class2_AI ai in AIs)
            {
                if (ai.Life <= 0)
                {
                    aIsToRemove.Add(ai);
                }
            }
            foreach (var ai in aIsToRemove)
            {
                AIs.Remove(ai);
            }
        }
        public void UpdateBombs()
        {
            foreach (Bomb bomb in Bombs.ToList())
            {
                if (DateTime.Now - bomb.PlaceTime > TimeSpan.FromSeconds(1))
                {
                    bomb.Explode(this);
                    Bombs.Remove(bomb);
                }
            }
        }













































    }
}